package HUZ;

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean isChar(char a) {
        if (a >= 'A' && a <= 'Z') {
            return true;
        }
        return false;
    }


    public static void main3(String[] args) {
        Map<String,Integer>map=new TreeMap<>();
        map.put("f",1);
        map.put("g",2);
        map.put("a",2);
        map.put("d",3);
        map.put("c",1);

        List<String>list=new ArrayList<>();
        Set<String> strings = map.keySet();
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        int[]arr=new int[]{3};
       entries.stream()
               .sorted(((o1, o2) -> o2.getValue()-o1.getValue()))
               .forEach(o->{
                   if(arr[0]-->0){
                       list.add(o.getKey());
                   }
               });
        System.out.println(list);
//        System.out.println();
//        Map<Integer,String>map2=new TreeMap<>();
//
    }

    public static void main2(String[] args) {
        String[]words=new String[]{"i", "love", "leetcode", "i", "love", "coding"};
        List<String> strings = topKFrequent(words, 2);
        System.out.println(strings);

    }

    public static List<String> topKFrequent(String[] words, int k) {
        TreeMap<String,Integer>map=new TreeMap<>();
        for(int i=0;i<words.length;i++){
            if(!map.containsValue(words[i])){
                map.put(words[i],1);
            }else{
                int counts=map.get(words[i]);
                map.put(words[i],counts++);
            }
        }
        int[]arr=new int[]{k};
        List<String>list=new ArrayList<>();
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        entries.stream()
                .sorted((entry,entry2)->entry.getValue()-entry2.getValue())
                .forEach(entry->{
                    if(arr[0]>0){
                        list.add(entry.getKey());
                        arr[0]--;
                    }
                });
        return list;
    }

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String right = in.nextLine();
            String fail = in.nextLine();
            TreeSet<Character> setRight = new TreeSet<>();
            TreeSet<Character> setFail = new TreeSet<>();
            //将输入的每个键进行去重存储
            for (int i = 0; i < right.length(); i++) {
                char ch = right.charAt(i);
                if(isChar(ch)){
                    String s=ch+"";
                    String s1 = s.toLowerCase();
                    //System.out.println(s1);
                    ch=s1.charAt(0);
                }
                setRight.add(ch);
            }
            //实际输出的键去重存储
            for (int i = 0; i < fail.length(); i++) {
               char ch = fail.charAt(i);
                if(isChar(ch)){
                    String s=ch+"";
                    String s1 = s.toLowerCase();
                    //System.out.println(s1);
                    ch=s1.charAt(0);
                }
                setFail.add(ch);
            }

            while (setFail.size() != 0) {
                char ch = setFail.pollFirst();
                setRight.remove(ch);
            }
            String target = "";
            right=right.toLowerCase(Locale.ROOT);
            for (int i = 0; i < right.length(); i++) {
                char ch = right.charAt(i);
                if (setRight.contains(ch)) {
                    target += ch + "";
                    setRight.remove(ch);
                }
            }


            System.out.println(target.toString().toUpperCase());
        }
    }
}